package com.freshmarket.freshmarket.service.impl;

import com.freshmarket.freshmarket.controller.form.CouponShowForm;
import com.freshmarket.freshmarket.entity.Coupon;
import com.freshmarket.freshmarket.dao.CouponMapper;
import com.freshmarket.freshmarket.entity.UserCoupon;
import com.freshmarket.freshmarket.exception.MyException;
import com.freshmarket.freshmarket.service.ICouponService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.freshmarket.freshmarket.utils.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Squirrel
 * @since 2020-09-04
 */
@Service
public class CouponServiceImpl implements ICouponService {

    @Resource
    private CouponMapper couponMapper;

    /**
     * 直接发放
     * @param couponShowForm
     * @return
     * @throws Exception
     */
    @Override
    public Result selectDirect(CouponShowForm couponShowForm) throws Exception {
        List<Coupon> coupons = couponMapper.selectByCouponStatus(couponShowForm.getCouponStatusOne());
        if(coupons != null){
            return new Result(true,200,"直接发放券查询成功！",coupons);
        }else{
            throw new MyException("发放券查询失败！");
        }

    }

    /**
     * 查询积分兑换的优惠券
     * @param couponShowForm
     * @return
     * @throws Exception
     */
    @Override
    public Result selectScore(CouponShowForm couponShowForm) throws Exception {

        List<Coupon> coupons = couponMapper.selectByCouponStatus(couponShowForm.getCouponStatusThree());
        if(coupons != null){
            return new Result(true,200,"积分优惠券查询成功！",coupons);
        }else{

            throw new MyException("积分优惠券查询失败！");
        }

    }

    //个人领取过的优惠券
    @Override
    public Result selectByIdCoupon(Integer userId, String couponStatus) throws Exception {
        List<Coupon> coupons = couponMapper.selectByIdCoupon(userId, couponStatus);
        return Result.successResult(coupons);
    }

}
